# coding=UTF-8
import datetime
import random
import time


import tushare as ts
from Manager import Messenger
# import pymysql

atime = time.strftime("今天是%Y年%m月%d日",time.localtime())
slist=['000858','600519','000568']
# 通知部分
# https://oapi.dingtalk.com/robot/send?access_token=9628772cbb96f5ef8a8bc8c3f4d0599edc26585cd0c773e46b1bfa7aa730ecde
# https://oapi.dingtalk.com/robot/send?access_token=fc99a0175355380f67ae7102d9acf7cce1e7911fa839ed1278112f4fab87eb63
m = Messenger(
   token="9628772cbb96f5ef8a8bc8c3f4d0599edc26585cd0c773e46b1bfa7aa730ecde",
   secret="hello123!"
)
m1  = Messenger(
   token="fc99a0175355380f67ae7102d9acf7cce1e7911fa839ed1278112f4fab87eb63",
   secret="hello123!"
)


def get_now_jiage(code):
   df = ts.get_realtime_quotes(code)[['name','open','price','pre_close','date','time']]
   return df

def pd_ztjytime():  # 判断是否是交易时间
   now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
   now_datetime = datetime.datetime.strptime(now_time, '%Y-%m-%d %H:%M:%S')
   d1 = datetime.datetime.strptime(datetime.datetime.now().strftime('%Y-%m-%d') + ' 11:30:01', '%Y-%m-%d %H:%M:%S')
   d2 = datetime.datetime.strptime(datetime.datetime.now().strftime('%Y-%m-%d') + ' 13:00:00', '%Y-%m-%d %H:%M:%S')
   delta1 = (now_datetime - d1).total_seconds()
   delta2 = (d2 - now_datetime).total_seconds()
   if delta1 > 0 and delta2 > 0:  # 在暂停交易的时间内
      return True  # 在暂停的交易时间范围内，返回 True
   else:
      return False  # 不在暂停的交易时间范围内，返回 False


time = datetime.datetime.now()
time = str(time)
nowhour=time[11:13]
nowmin = time[14:16]

msg=[]
# 测试数据
# msg.append(atime+',上午的价格为:')
# for l in slist:
#    df = get_now_jiage(l)
#    msg.append(df.loc[0, 'name'] + ',开盘价是:'+df.loc[0, 'open'] +'元，现在价格是:'+df.loc[0, 'price']+'元')

# 生成股票数据
if int(nowhour)==11 and int(nowmin)==30:
   msg.append(atime+',上午的情况为:')
   for l in slist:
      df = get_now_jiage(l)
      msg.append(df.loc[0, 'name'] + ',开盘价是:' + df.loc[0, 'open'] + '元，现在价格是:' + df.loc[0, 'price'] + '元')
if int(nowhour)==15 and int(nowmin)==30:
   msg.append(atime+',下午的情况为:')
   for l in slist:
      df = get_now_jiage(l)
      msg.append(df.loc[0, 'name'] + ',开盘价是:' + df.loc[0, 'open'] + '元，现在价格是:' + df.loc[0, 'price'] + '元')

print('\n'.join(msg))

m.send_text('\n'.join(msg))
m1.send_text('\n'.join(msg))

# 古诗推送
# db = pymysql.connect(host='bj-cynosdbmysql-grp-9ojd982i.sql.tencentcdb.com',
#                      port=24051,
#                      user='root',
#                      password='Hello123!',
#                      database='child',
#                      charset='utf8')

# 使用 cursor() 方法创建一个游标对象 cursor
# cursor = db.cursor()
#
# # 使用 execute()  方法执行 SQL 查询
# cursor.execute("SELECT VERSION()")
#
# # 使用 fetchone() 方法获取单条数据.
# data = cursor.fetchone()
#
# print("数据库连接成功！")
#
# # 关闭数据库连接
# db.close()
# if int(nowhour)==6 and int(nowmin)==30:
#    msg2=[]
#    a = list(range(1,65))
#    third = random.sample(a,3)
#
#    print(a)
#    print(third)
#
#    sql = "select poetry from old_poetry where no in("+str(third[0])+","+str(third[1])+","+str(third[2])+")"
#    try:
#       cursor = db.cursor()
#       cursor.execute(sql)
#       result = cursor.fetchall()
#
#       for data in result:
#          msg2.append(data[0])
#          print(data[0])
#          msg2.append('==================')
#    except Exception:
#       print("查询失败")
#
#    db.close()
#    print(msg2)
#
#    m1.send_text('\n'.join(msg2))